package leetcode.code0712;

import leetcode.IDebug;

public class Solution implements IDebug {

	public int minimumDeleteSum(String s1, String s2) {
		char[] cs1 = s1.toCharArray();
		char[] cs2 = s2.toCharArray();
		int len1 = cs1.length;
		int len2 = cs2.length;
		int[][] dp = new int[len1][len2];
		dp[0][0] = cs1[0] == cs2[0] ? 0 : (int) cs1[0] + (int) cs2[0];
		for (int i = 1; i < len2; i++) {
			if (cs2[i] == cs1[0]) {
				dp[0][i] = dp[0][i - 1] - (int) cs2[i];
			} else {
				dp[0][i] = dp[0][i - 1] + (int) cs2[i];
			}
		}
		for (int i = 1; i < len1; i++) {
			if (cs1[i] == cs2[0]) {
				dp[i][0] = dp[i - 1][0] - (int) cs1[i];
			} else {
				dp[i][0] = dp[i - 1][0] + (int) cs1[i];
			}
		}
		for (int i = 1; i < len1; i++) {
			char c1 = cs1[i];
			for (int j = 1; j < len2; j++) {
				char c2 = cs2[j];
				if (c1 == c2) {
					dp[i][j] = dp[i - 1][j - 1];
				} else {
					dp[i][j] = Math.min(dp[i - 1][j] + (int) c1, dp[i][j - 1] + (int) c2);
				}
			}
		}
		return dp[len1 - 1][len2 - 1];
	}

	@Override
	public void debug4() {
		// TODO Auto-generated method stub

	}

	@Override
	public void debug3() {
		// TODO Auto-generated method stub

	}

	@Override
	public void debug2() {
		// TODO Auto-generated method stub

	}

	@Override
	public void debug1() {
		// TODO Auto-generated method stub

	}

	public static void main(String[] args) {
		Solution so = new Solution();
		so.debug1();
		so.debug2();
		so.debug3();
		so.debug4();
		System.out.println((int) 's');
	}

}
